package com.entor.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.entor.model.CocAndInfo;
import com.entor.model.Cocs;
import com.entor.model.Files;
import com.entor.model.Msg;
import com.entor.service.CocsService;
import com.entor.service.FilesService;
import com.entor.util.FileIOUtil;

@RequestMapping("/cc")
@Controller
public class CocsController extends BaseController<Cocs>{

	@Autowired
	private CocsService cs;
	@Autowired
	private FilesService fs;
	
	@RequestMapping("/index")
	public String index(){
		return "/cocs/index";
	}
	
	@RequestMapping("/aoe")
	public String aoe(){
		return "/cocs/addOrEdit";
	}
	
	@Override
	public List<Cocs> find(Cocs t) {
		return cs.find(t);
	}
	
	@RequestMapping("/saveInfo")
	@ResponseBody
	public Msg saveInfo(CocAndInfo ani,
			@RequestParam(required=false,value="photo") MultipartFile[] files)
		throws Exception{
		Msg m = cs.saveInfo(ani);
		if(m.isIs()){
			List<Files> list = 
					FileIOUtil.saveFile(files, "t_coc_coc", ((Cocs)m.getObj()).getId());
			if(list.size()>0) fs.insert(list.get(0));
		}
		
		return m;
	}
}
